﻿using System.Data.SQLite;
using DVDTek_Data_Object.Accessor;

namespace DVDTek_Data_Object.DataObject
{
    public class TypeDataObject
    {
        #region Members
        private int id = -1;
        private string type = string.Empty;

        public int Id
        {
            get { return this.id; }
        }
        public string Type
        {
            get { return this.type; }
            set
            {
                TypeDataObject type = TypeAccessor.Instance.Select(value);
                if ((type == null) || (type.Id == this.Id))
                    this.type = value;
                else if (this.Id == -1)
                {
                    this.id = type.id;
                    this.type = value;
                }
                else
                    throw new SQLiteException("Value is already use by another record");
            }
        }
        #endregion

        #region Constructors
        public TypeDataObject()
        {
            this.id = -1;
            this.type = string.Empty;
        }
        public TypeDataObject(string type)
        {
            this.id = -1;
            this.Type = type;
        }
        public TypeDataObject(int id, string type)
        {
            this.id = id;
            this.Type = type;
        }
        #endregion

        public TypeDataObject Copy()
        {
            return new TypeDataObject(this.Id, this.Type);
        }

        public void Update()
        {
            if (this.id == -1)
                this.id = TypeAccessor.Instance.Insert(this.Type);
            else
                TypeAccessor.Instance.Update(this.Id, this.Type);
        }

        public void Delete()
        {
            if (this.id != -1)
            {
                TypeAccessor.Instance.Delete(this.Id);
                this.id = -1;
            }
        }

        public static TypeDataObject GetTypeDataObject(int id)
        {
            return TypeAccessor.Instance.Select(id);
        }
    }
}
